package cn.wowjoy.pda.android_pharmacy.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.ui.listener.RecyclerItemClickListener;

/**
 * Created by panxinyu on 2017/7/14.
 */

public class PharmacyAdapter extends RecyclerView.Adapter<PharmacyAdapter.MyHolder>  {
   private String[] mStrings;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private RecyclerItemClickListener mOnItemClickListener;

    public PharmacyAdapter(String[] strings, Context context) {
        mStrings = strings;
        mContext = context;
        mLayoutInflater=LayoutInflater.from(mContext);
    }
    public PharmacyAdapter(String[] strings, Context context,RecyclerItemClickListener mOnItemClickListener) {
        mStrings = strings;
        mContext = context;
        mLayoutInflater=LayoutInflater.from(mContext);
        this.mOnItemClickListener=mOnItemClickListener;
    }
    public void setOnItemClickLitener(RecyclerItemClickListener mOnItemClickLitener)
    {
        this.mOnItemClickListener = mOnItemClickLitener;
    }
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyHolder myHolder=new MyHolder(mLayoutInflater.inflate(R.layout.item_rv_choose_pharmacy,parent,false));
        return myHolder;
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, int position) {
       if(mStrings !=null && mStrings.length != 0){
           holder.mImageView.setImageResource(R.mipmap.ic_launcher);
           holder.mTextView.setText(mStrings[position]);
       }
       if( mOnItemClickListener != null){
           holder.itemView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   int pos = holder.getLayoutPosition();
                   mOnItemClickListener.OnItemClick(holder.itemView, pos);
//                   if(pos == 0){
//                       holder. mRelativeLayout.setSelected(true);
//                   }
               }
           });
       }
    }

    @Override
    public int getItemCount() {
        return mStrings.length;
    }



    class MyHolder extends RecyclerView.ViewHolder{
        ImageView mImageView;
        TextView mTextView;
        RelativeLayout mRelativeLayout;
        public MyHolder(View itemView) {
            super(itemView);
            mImageView= (ImageView) itemView.findViewById(R.id.imgv_item_rv_pharmacy_name);
            mTextView= (TextView) itemView.findViewById(R.id.tv_item_rv_pharmacy_name);
            mRelativeLayout= (RelativeLayout) itemView.findViewById(R.id.rl_item_choose_phramacy);
        }
    }
}
